from marshmallow import fields


class Sender(fields.Str):
    """
    示例 "101:小新"
    """
    def _deserialize(self, *args, **kwargs):
        raw_value = super()._deserialize(*args, **kwargs)
        user_id, name = raw_value.split(':', 1)
        value = {'id': user_id, 'name': name}
        return value


class Receiver(fields.Str):
    """
    示例 "T|S|@100,101"
    """
    def _deserialize(self, *args, **kwargs):
        raw_value = super()._deserialize(*args, **kwargs)
        value = {'groups': set(), 'users': set()}
        for item in raw_value.split('|'):
            if not item:
                continue
            if not item.startswith('@'):
                value['groups'].add(item)
                continue
            for member in item.lstrip('@').split(','):
                if not member:
                    continue
                value['users'].add(member)
        return value


class Token(fields.Str):
    """TODO： 未来会对token进行特殊校验
    """
    pass
